/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Discipline;
import model.Main;
import model.Teacher;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import service.DisciplineService;
import service.TeacherService;

/** 
 *
 * @author Moraes
 */
public class MainController extends SimpleFormController {

    private TeacherService teacherService;
    private DisciplineService disciplineService;

    public void setDisciplineService(DisciplineService disciplineService) {
        this.disciplineService = disciplineService;
    }

    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    public MainController() {
        //Initialize controller properties here or 
        //in the Web Application Context

        setCommandClass(Main.class);
        setCommandName("mainCommand");
        setSuccessView("successView");
        setFormView("mainView");

    }

//    @Override
//    protected void doSubmitAction(Object command) throws Exception {
//        throw new UnsupportedOperationException("Not yet implemented");
//    }
    //Use onSubmit instead of doSubmitAction 
    //when you need access to the Request, Response, or BindException objects
    /*
    @Override
    protected ModelAndView onSubmit(
    HttpServletRequest request,
    HttpServletResponse response,
    Object command,
    BindException errors) throws Exception {
    ModelAndView mv = new ModelAndView(getSuccessView());
    //Do something...
    return mv;
    }
     */
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView(getFormView());
        
        List<Teacher> teacherList = this.teacherService.getAll();
        List<Discipline> disciplineList = this.disciplineService.getAll();

        mv.addObject("teacherList", teacherList);
        mv.addObject("disciplineList", disciplineList);
        
        return mv;
    }
}
